package com.zjgsu.nftplatform.service.impl;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import com.zjgsu.nftplatform.service.PurchaseService;
import com.zjgsu.nftplatform.util.PostUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

/**
 * @author ahy231
 * @date 2022/4/22 17:44
 * @description
 */
@Service
public class PurchaseServiceImpl implements PurchaseService {

    private final RestTemplate restTemplate;
    private final PostUtil postUtil;

    @Value("${myurl.go-server}")
    private String server;

    @Value("${go-server.secret}")
    private String secret;

    public PurchaseServiceImpl(RestTemplate restTemplate, PostUtil postUtil) {
        this.restTemplate = restTemplate;
        this.postUtil = postUtil;
    }

    @Override
    public String searchPublication(String cid) {
        byte[] bytes = restTemplate.getForObject("http://" + server + ":8069/purchase?method={method}&cid={cid}&secret={secret}", byte[].class, "searchPublication", cid, secret);
        Assert.notNull(bytes, "Failed to get publication from server.");
        return new String(bytes, Charset.defaultCharset()).replace("\0", "");
    }

    @Override
    public String getPublicationById(String pid) {
        byte[] bytes = restTemplate.getForObject("http://" + server + ":8069/purchase?method={method}&pid={pid}&secret={secret}", byte[].class, "getPublication", pid, secret);
        Assert.notNull(bytes, "Failed to get publication from server.");
        return new String(bytes, Charset.defaultCharset()).replace("\0", "");
    }

    @Override
    public String getStakeById(String pid, String uaddr) {
        byte[] bytes = restTemplate.getForObject("http://" + server + ":8069/purchase?method={method}&pid={pid}&uaddr={uaddr}&secret={secret}", byte[].class, "getStake", pid, uaddr, secret);
        Assert.notNull(bytes, "Failed to get publication from server.");
        return new String(bytes, Charset.defaultCharset()).replace("\0", "");
    }

    @Override
    public String getPriceById(String pid, String uaddr) {
        byte[] bytes = restTemplate.getForObject("http://" + server + ":8069/purchase?method={method}&pid={pid}&uaddr={uaddr}&secret={secret}", byte[].class, "getPrice", pid, uaddr, secret);
        Assert.notNull(bytes, "Failed to get publication from server.");
        return new String(bytes, Charset.defaultCharset()).replace("\0", "");
    }

    @Override
    public Boolean isSeller(String cid, String uaddr) {
        byte[] bytes = restTemplate.getForObject("http://" + server + ":8069/purchase?method={method}&cid={cid}&uaddr={uaddr}&secret={secret}", byte[].class, "isSeller", cid, uaddr, secret);
        Assert.notNull(bytes, "Failed to get publication from server.");
        return new String(bytes, Charset.defaultCharset()).replace("\0", "").equals("true");
    }

    @Override
    public String createPublication(String cid, String dayTime, String address, String password) {
        return postUtil.purchasePost(MapUtil.of(
                Pair.of("method", "createPublication"),
                Pair.of("cid", cid),
                Pair.of("day_time", dayTime),
                Pair.of("address", address),
                Pair.of("password", password)
        ));
    }

    @Override
    public String setStake(String pid, String stake, String address, String password) {
        return postUtil.purchasePost(MapUtil.of(
                Pair.of("method", "setStake"),
                Pair.of("pid", pid),
                Pair.of("stake", stake),
                Pair.of("address", address),
                Pair.of("password", password)
        ));
    }

    @Override
    public String setPrice(String pid, String price, String address, String password) {
        return postUtil.purchasePost(MapUtil.of(
                Pair.of("method", "setPrice"),
                Pair.of("pid", pid),
                Pair.of("price", price),
                Pair.of("address", address),
                Pair.of("password", password)
        ));
    }

    @Override
    public String agreePublication(String id, String price, String address, String password) {
        return postUtil.purchasePost(MapUtil.of(
                Pair.of("method", "agreePublication"),
                Pair.of("id", id),
                Pair.of("price", price),
                Pair.of("address", address),
                Pair.of("password", password)
        ));
    }
}
